import React from 'react';
import { history } from 'umi'
import { message } from 'antd';

import { getUserInfo, parseLocation } from '@/utils/utils';
import { hasWorkFlowByType } from '@/pages/WorkFlow/utils';
import { useRequest } from '@/hooks/';
import { queryProcess } from '@/pages/WorkFlow/Flow/services';
import { getProcessInfo } from '@/pages/Recruitment/service';

// const queryToString = (obj = {}) =>
//     Object.keys(obj)
//         .map(item => `${item}=${obj[item]}`)
//         .join('&');

const useWorkFlow = ({ launchParam = {} }) => {
    const { employeeCode } = getUserInfo();
    const { type } = launchParam;

    const launch = React.useCallback(
        async (query = {}, routerOption = {}) => {
            const { workFlowUrl } = await hasWorkFlowByType(type);
            if (!workFlowUrl) {
                return;
            }
            const [pathname, defaultQuery] = parseLocation({ pathname: workFlowUrl });
            history.push({
                pathname,
                query: { ...defaultQuery, ...query },
                ...routerOption
            });
        },
        [type]
    );

    const { run: fetchProcessInfo } = useRequest(({ processInstanceId }) => getProcessInfo(processInstanceId), {
        manual: true,
        onSuccess: (res, param = []) => {
            const { code, data: resData, msg } = res ?? {};
            const { processInstanceName } = param[0] ?? {};
            const routerOption = param[1] ?? {};
            if (code !== 10000) {
                message.error(msg);
                return;
            }
            history.push({
                pathname: '/personal/portal/waitApproval/approvalDetail',
                query: {
                    taskId: resData?.id,
                    processInstanceName
                },
                ...routerOption
            });
        }
    });

    const { run: view } = useRequest(({ processInstanceId }) => queryProcess({ processInstanceId }), {
        manual: true,
        onSuccess: (res, param = []) => {
            const { processInstanceId, processInstanceName } = param[0] ?? {};
            const routerOption = param[1] ?? {};
            const { code, data: resData, msg } = res ?? {};
            if (code !== 10000) {
                message.error(msg);
                return;
            }
            const judgeUser = resData?.find(item => item.type === '审批人' && item.status === 2);
            // 判断当前用户是审批人，并且当前处于审批中状态
            if (judgeUser?.userList?.includes(employeeCode)) {
                fetchProcessInfo({ processInstanceId }, routerOption);
                return;
            }
            const initialUser = resData?.find(item => item.type === '发起人');
            // 判断当前用户是发起人
            if (initialUser?.userList?.includes(employeeCode)) {
                history.push({
                    pathname: '/personal/portal/myApplication/detail',
                    query: {
                        processInstanceId,
                        processInstanceName
                    },
                    ...routerOption
                });
                return;
            }
            history.push({
                pathname: '/personal/portal/myAuditRead/myAuditReadDetail',
                query: {
                    processInstanceId,
                    processInstanceName
                },
                ...routerOption
            });
        }
    });

    return { launch, view };
};

export default useWorkFlow;
